#ifndef _WORKER_POOL_H_
#define _WORKER_POOL_H_

#include "work_queue.h"

#ifdef __cplusplus
extern "C"{
#endif

typedef struct worker_pool worker_pool_t;

struct worker_pool {
    uint8_t worker_nums;
    worker_t *workers[0xff];
	worker_t *hi_worker; /* only for high priority work*/
	mutex_t lock;
};

int worker_pool_init(worker_pool_t *self, uint8_t nums);
int worker_pool_term(worker_pool_t *self);
// worker_t *worker_pool_choose(worker_pool_t *self);
void worker_pool_add(worker_pool_t *self, work_t *w);
void worker_pool_hi_add(worker_pool_t *self, work_t *w);

static inline worker_pool_t *worker_pool_new(uint8_t nums)
{
    worker_pool_t *self = (worker_pool_t *)MALLOC(sizeof(worker_pool_t));
    if (self) {
        if (worker_pool_init(self, nums) < 0)
            safe_free(self);
    }

    return self;
}

static inline void worker_pool_destroy(worker_pool_t **p_self)
{
    if (p_self && *p_self) {
        worker_pool_t *self = *p_self;
        worker_pool_term(self);
        safe_free(*p_self);
    }
}

#ifdef __cplusplus
}
#endif

#endif
